package com.ebao.action.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.ebao.action.base.BaseAction;
import com.ebao.constants.UserType;
import com.ebao.constants.WebConstants;
import com.ebao.po.doctor.TDoctor;
import com.ebao.po.talk.TTalk;
import com.ebao.po.talk.TTalkComment;
import com.ebao.po.user.TUser;
import com.ebao.service.talk.ITalkComComService;
import com.ebao.service.talk.ITalkCommentService;
import com.ebao.service.talk.ITalkPushService;
import com.ebao.service.talk.ITalkService;
import com.ebao.vo.Talk;

/**
 * 说说
 * 
 * @author snow
 * @Date 2012-9-3
 */
public class TalkAction extends BaseAction {

	private static final long serialVersionUID = 6608878186107704133L;
	
	@Resource
	private ITalkService talkService;
	@Resource
	private ITalkPushService talkPushService;
	@Resource
	private ITalkCommentService talkCommentService;
	@Resource
	private ITalkComComService talkComComService;
	
	private String jsonInfo;
	
	private String id;
	private String content;
	
	/**
	 * 用户发表说说
	 */
	@Action(value = "user-talk", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String userTalk(){
		TUser user = (TUser) session.get(WebConstants.LOGIN_USER);
		TTalk talk = new TTalk();
		talk.setContent(content);
		talk.setTalkDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		talk.setUserType(UserType.COMMON_USER);
		talk.setReferId(user.getId());
		talkService.save(talk);
		
		// 推送说说
		talkPushService.userPush(user, talk);
		
		// 返回页面
		Talk tk = new Talk();
		tk.setReferId(user.getId());
		tk.setName(user.getUserName());
		tk.setHeadUrl(user.getHeads());
		tk.setId(talk.getId());
		tk.setContent(talk.getContent());
		tk.setTalkDate(talk.getTalkDate());
		tk.setUserType(talk.getUserType());
		
		JSONObject obj = JSONObject.fromObject(tk);
		jsonInfo = obj.toString();
		
		return SUCCESS;
	}

	/**
	 * 用户点击查看更多说说
	 */
	@Action(value = "user-more-talk", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String userMoreTalk() {
		TUser user = (TUser) session.get(WebConstants.LOGIN_USER);
		
		
		
		
		jsonInfo = "";
		
		return SUCCESS;
	}
	
	/**
	 * 用户定时获取新发布的说说条数
	 */
	@Action(value = "user-fresh-talk-count", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String userFreshTalkCount() {
		TUser user = (TUser) session.get(WebConstants.LOGIN_USER);
		String loginTime = (String) session.get("loginTime");
		int count = talkService.getUserTalkFreshCount(user, loginTime);
		jsonInfo = count + "";
		
		return SUCCESS;
	}
	
	/**
	 * 用户获取新发布的说说
	 */
	@Action(value = "user-fresh-talk", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String userFreshTalk() {
		TUser user = (TUser) session.get(WebConstants.LOGIN_USER);
		String loginTime = (String) session.get("loginTime");
		List<Talk> talkList = talkService.getUserTalkFresh(user, loginTime);
		// 每次取完之后重设该属性的值
		session.put("loginTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
		JSONObject obj = JSONObject.fromObject(talkList);
		jsonInfo = obj.toString();
		
		return SUCCESS;
	}
	
	/**
	 * 用户发表评论
	 */
	@Action(value = "user-talk-comment", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String userTalkComment() {
		TUser user = (TUser) session.get(WebConstants.LOGIN_USER);
		TTalk talk = talkService.getById(id);
		
		TTalkComment talkComment = new TTalkComment();
		talkComment.setTalk(talk);
		talkComment.setContent(content);
		talkComment.setCommentDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		talkComment.setUserType(UserType.COMMON_USER);
		talkComment.setReferId(user.getId());
		
		/***************方式一,更新TTalk*****************/
//		List<TTalkComment> commentList = new ArrayList<TTalkComment>();
//		commentList.add(talkComment);
//		talk.setTalkComment(commentList);
//		talkService.update(talk);	//会级联保存TTalkComment?
		
		/***************方式二,仅保存TTalkComment*****************/
		talkCommentService.save(talkComment);
		
		/***************测试*****************/
		List<TTalkComment> commentLists = talk.getTalkComment();
		if(CollectionUtils.isNotEmpty(commentLists)){
			for(TTalkComment tc : commentLists){
				System.out.println(tc.getContent());
			}
		} else {
			System.out.println("-----------保存用户评论失败-----------");
		}
		
		
		// 返回页面
//		Talk tk = new Talk();
//		tk.setReferId(user.getId());
//		tk.setName(user.getUserName());
//		tk.setHeadUrl(user.getHeads());
//		tk.setId(talk.getId());
//		tk.setContent(talk.getContent());
//		tk.setTalkDate(talk.getTalkDate());
//		tk.setUserType(talk.getUserType());
//		
//		JSONObject obj = JSONObject.fromObject(tk);
//		jsonInfo = obj.toString();
		
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 医生发表说说
	 */
	@Action(value = "doctor-talk", results = {@Result(name = SUCCESS, 
			type = "json", params = {"ignoreHierarchy", "false", "root","jsonInfo"})})
	public String doctorTalk(){
		TDoctor doctor = (TDoctor) session.get(WebConstants.LOGIN_DOCTOR);
		TTalk talk = new TTalk();
		talk.setContent(content);
		talk.setTalkDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		talk.setUserType(UserType.DOCTOR);
		talk.setReferId(doctor.getId());
		talkService.save(talk);
		
		// 推送说说
		talkPushService.doctorPush(doctor, talk);
		
		// 返回页面
		Talk tk = new Talk();
		tk.setReferId(doctor.getId());
		tk.setName(doctor.getUserName());
		tk.setHeadUrl(doctor.getHeads());
		tk.setId(talk.getId());
		tk.setContent(talk.getContent());
		tk.setTalkDate(talk.getTalkDate());
		tk.setUserType(talk.getUserType());
		
		JSONObject obj = JSONObject.fromObject(tk);
		jsonInfo = obj.toString();
		
		return SUCCESS;
	}
	
	/**
	 * doctor定时获取新发布的说说条数
	 */
	@Action(value = "doctor-fresh-talk-count", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String doctorFreshTalkCount() {
		TDoctor doctor = (TDoctor) session.get(WebConstants.LOGIN_DOCTOR);
		String loginTime = (String) session.get("loginTime");
		int count = talkService.getDoctorTalkFreshCount(doctor, loginTime);
		jsonInfo = count + "";
		
		return SUCCESS;
	}
	
	/**
	 * doctor获取新发布的说说
	 */
	@Action(value = "doctor-fresh-talk", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String doctorFreshTalk() {
		TDoctor doctor = (TDoctor) session.get(WebConstants.LOGIN_DOCTOR);
		String loginTime = (String) session.get("loginTime");
		List<Talk> talkList = talkService.getDoctorTalkFresh(doctor, loginTime);
		// 每次取完之后重设该属性的值
		session.put("loginTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
		JSONObject obj = JSONObject.fromObject(talkList);
		jsonInfo = obj.toString();
		
		return SUCCESS;
	}

	public String getJsonInfo() {
		return jsonInfo;
	}

	public void setJsonInfo(String jsonInfo) {
		this.jsonInfo = jsonInfo;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
}
